﻿using System;

namespace SBPweb.Talk.Client.Library
{

    public class ConnectionManager : ClientManagerBase
    {

        #region Protected fields

        protected agsXMPP.Xmpp.Client.XmppClient client;
        protected agsXMPP.Jid jid;

        #endregion

        #region Constructor logic

        internal ConnectionManager(ChatClient parent, string host, int port, string resource)
            : base(parent)
        {
            client = new agsXMPP.Xmpp.Client.XmppClient();
            client.Hostname = host;
            client.Port = port;
            client.Resource = resource;
            InitializeHandlers();
        }

        ~ConnectionManager()
        {
            FinalizeHandlers();
        }

        #endregion

        #region Property accessors

        internal agsXMPP.Xmpp.Client.XmppClient XmppClient
        {
            get
            {
                return client;
            }
        }

        #endregion

        #region Methods

        public override void InitializeHandlers()
        {
            client.OnAuthError += new EventHandler<agsXMPP.Xmpp.Sasl.SaslEventArgs>(client_OnAuthError);
            client.OnClose += new EventHandler<agsXMPP.EventArgs>(client_OnClose);
            client.OnError += new EventHandler<agsXMPP.ExceptionEventArgs>(client_OnError);
            client.OnLogin += new EventHandler<agsXMPP.EventArgs>(client_OnLogin);
            client.OnStreamError += new EventHandler<agsXMPP.StreamErrorEventArgs>(client_OnStreamError);
            client.OnXmlError += new EventHandler<agsXMPP.ExceptionEventArgs>(client_OnXmlError);
        }

        public override void FinalizeHandlers()
        {
            client.OnAuthError -= new EventHandler<agsXMPP.Xmpp.Sasl.SaslEventArgs>(client_OnAuthError);
            client.OnClose -= new EventHandler<agsXMPP.EventArgs>(client_OnClose);
            client.OnError -= new EventHandler<agsXMPP.ExceptionEventArgs>(client_OnError);
            client.OnLogin -= new EventHandler<agsXMPP.EventArgs>(client_OnLogin);
            client.OnStreamError -= new EventHandler<agsXMPP.StreamErrorEventArgs>(client_OnStreamError);
            client.OnXmlError -= new EventHandler<agsXMPP.ExceptionEventArgs>(client_OnXmlError);
        }

        public void SignIn(string userName, string password)
        {
            if (SigningIn != null)
            {
                SigningIn(this, EventArgs.Empty);
            }
            jid = new agsXMPP.Jid(userName, client.Hostname, client.Resource);
            client.Username = jid.User;
            client.Password = password;
            client.Compression = true;
            client.Open();
        }

        public void SignOut()
        {
            if (SigningOut != null)
            {
                SigningOut(this, EventArgs.Empty);
            }
            client.Close();
        }

        #endregion

        #region Events

        public event EventHandler SigningIn;

        public event EventHandler SignedIn;

        public event EventHandler SigningOut;

        public event EventHandler SignedOut;

        #endregion

        #region Event handlers

        private void client_OnAuthError(object sender, agsXMPP.Xmpp.Sasl.SaslEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void client_OnError(object sender, agsXMPP.ExceptionEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void client_OnXmlError(object sender, agsXMPP.ExceptionEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void client_OnStreamError(object sender, agsXMPP.StreamErrorEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void client_OnLogin(object sender, agsXMPP.EventArgs e)
        {
            if (SignedIn != null)
            {
                SignedIn(this, e);
            }
        }

        private void client_OnClose(object sender, agsXMPP.EventArgs e)
        {
            if (SignedOut != null)
            {
                SignedOut(this, e);
            }
        }

        #endregion

    }

}